package com.currency.mall.ui.my;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.currency.mall.Constant;
import com.currency.mall.R;
import com.currency.mall.event.MainEvent;
import com.currency.mall.netWork.NetWork;
import com.currency.mall.util.IDCard;
import com.mifengs.commonbase.base.BaseActivity;
import com.mifengs.commonbase.util.PreferenceUtils;
import com.mifengs.commonbase.util.StatusBarCompat;
import com.mifengs.commonbase.util.StatusBarUtils;
import com.mifengs.commonbase.util.ToastUitl;
import com.mifengs.commonbusines.entity.ResponseBean;

import org.greenrobot.eventbus.EventBus;

import java.text.ParseException;
import java.util.HashMap;
import java.util.concurrent.ExecutionException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/12/24.
 */

public class NameAutoActivity extends BaseActivity {

    @BindView(R.id.et_input_name)
    EditText etInputName;
    @BindView(R.id.et_input_sfz)
    EditText etInputSfz;
    @BindView(R.id.btn_submit)
    Button btnSubmit;
    private int type = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_name_auto);
        ButterKnife.bind(this);
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(getContext(), R.color.title_color));
        StatusBarUtils.StatusBarLightMode(this);
        etInputName.addTextChangedListener(textWatcher);
        etInputSfz.addTextChangedListener(textWatcher);
        btnSubmit.getBackground().setAlpha(50);
        btnSubmit.setClickable(false);
        type = getIntent().getIntExtra("type",0);
    }

    @OnClick({R.id.ll_back, R.id.btn_submit})
    public void onViewClicked(View view){
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.btn_submit:
                try {
                    String str = IDCard.IDCardValidate(etInputSfz.getText().toString().trim());
                    if("".equals(str)){
                        authentication();
                    }else{
                        ToastUitl.show(str,200);
                    }

                }catch (Exception e){

                }

                break;
        }
    }

    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String name = etInputName.getText().toString().trim();
            String idCard = etInputSfz.getText().toString().trim();
            if (TextUtils.isEmpty(name) || TextUtils.isEmpty(idCard) || TextUtils.isEmpty(s)) {
                btnSubmit.setBackgroundResource(R.drawable.btn_bg);
                btnSubmit.getBackground().setAlpha(50);
                btnSubmit.setClickable(false);
            } else {
                btnSubmit.setBackgroundResource(R.drawable.btn_bg);
                btnSubmit.getBackground().setAlpha(250);
                btnSubmit.setClickable(true);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

    public void authentication() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("name", etInputName.getText().toString().trim());
        hashMap.put("idCard", etInputSfz.getText().toString().trim());
        NetWork.getUserApi().identityAuthentication(hashMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        if (responseBean.getCode().equals(Constant.SUCCESS_CODE)) {
                            PreferenceUtils.saveUserName(getContext(), etInputName.getText().toString().trim());
                            ToastUitl.show("认证成功", 200);
                            if(type == 1){
                                setResult(1001);
                            }
                            EventBus.getDefault().post(new MainEvent(MainEvent.TAB_MY));
                            finish();
                        } else {
                            ToastUitl.show(responseBean.getMsg(), 200);
                        }
                    }
                });
    }
}
